<template>
  <div id="login">
    <!-- 图标固定在左上角 -->
    <div class="logo-container">
      <img src="../../image/image.png" height="61" width="215"  class="login-logo" alt="系统Logo"/>
    </div>

    <el-form ref="ruleFormRef" :model="loginfrom" :rules="rules" label-width="80px" class="form-card">
      <div class="form-header">
        <h3>登录</h3>
        <p class="form-desc">请输入账号密码登录系统</p>
      </div>

      <el-form-item label="账号" prop="username">
        <el-input v-model="loginfrom.username" placeholder="请输入账号" prefix-icon="User" class="form-input" />
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input v-model="loginfrom.password" type="password" show-password placeholder="请输入密码" prefix-icon="Lock"
          class="form-input" />
      </el-form-item>

      <div class="form-actions">
        <el-button type="success" @click="handleLogin()" class="login-btn" :loading="isLoading">
          <template #default>
            <span v-if="!isLoading">登录</span>
            <span v-else>登录中...</span>
          </template>
        </el-button>
        <el-button type="primary" @click="register()" class="register-btn">
          注册
        </el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { loginJS } from '@/api/user/user'
import router from '@/router'
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'

// 加载状态
const isLoading = ref(false)
const ruleFormRef = ref()
const loginfrom = reactive({
  username: "",
  password: ""
})

const rules = reactive({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 10, message: '账号长度3~10', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 10, message: '密码长度3~10', trigger: 'blur' },
  ],
})

const handleLogin = async () => {
  if (!ruleFormRef.value) return

  try {
    isLoading.value = true
    await ruleFormRef.value.validate()
    const res = await loginJS(loginfrom)
    const msg = typeof res === 'string' ? res : res?.data

    if (msg === "登陆成功" || msg === "登录成功") {
      localStorage.setItem("username", loginfrom.username)
      ElMessage.success('登陆成功，正在跳转...')
      setTimeout(() => {
        router.push("/meun")
      }, 800)
    } else {
      ElMessage.error(msg || '登录失败，请重试')
    }
  } catch (error) {
    if (error.name === 'ValidationError') {
      ElMessage.warning('请检查输入的账号密码格式')
    } else {
      ElMessage.error('网络请求失败，请稍后再试')
      console.error('登录错误:', error)
    }
  } finally {
    isLoading.value = false
  }
}

const register = () => {
  router.push("/register")
}
</script>

<style scoped>
#login {
  background: url("https://omo-oss-image.thefastimg.com/portal-saas/new2022111121264844598/cms/image/cceaa502-be4f-4c5a-86cf-2a5c8db9665c.jpg") no-repeat center/cover;
  height: 100vh;
  width: 100%;
  position: fixed;
}

/* Logo样式保持与注册页一致 */
.logo-container {
  margin: 30px;
  position: relative;
  z-index: 10;
}

.login-logo {
  height: 50px;
  width: 200px;
  cursor: pointer;
  transition: transform 0.3s ease;
}

.login-logo:hover {
  transform: scale(1.05);
}

/* 表单卡片大小与注册页统一 */
.form-card {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: rgba(255, 255, 255, 0.92);
  padding: 30px 40px;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  width: 100%;
  max-width: 400px;
  /* 与注册页保持一致的最大宽度 */
  backdrop-filter: blur(8px);
}

/* 表单头部标题 表单 调整表单内部元素大小，与注册页保持一致
 */
.form-header {
  text-align: center;
  margin-bottom: 25px;
}

.form-header h3 {
  font-size: 22px;
  color: #333;
  margin-bottom: 8px;
  font-weight: 600;
}

.form-desc {
  color: #666;
  font-size: 14px;
  margin: 0;
}

/* 输入框大小统一化 */
.form-input {
  width: 100%;
  /* 与注册页保持一致，移除固定200px宽度 */
  height: 42px;
  /* 固定输入框高度统一 */
  border-radius: 6px;
}

/* 按钮区域样式统一 */
.form-actions {
  display: flex;
  gap: 15px;
  margin-top: 20px;
}

.login-btn,
.register-btn {
  flex: 1;
  /* 按钮宽度平分 */
  height: 42px;
  /* 按钮高度统一 */
  font-size: 15px;
  transition: all 0.2s ease;
  border-radius: 6px;
}

.login-btn {
  background-color: #52c41a;
  border-color: #52c41a;
}

.login-btn:hover {
  background-color: #4aa919;
  border-color: #4aa919;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(82, 196, 26, 0.2);
}

.register-btn {
  background-color: #1890ff;
  border-color: #1890ff;
}

.register-btn:hover {
  background-color: #096dd9;
  border-color: #096dd9;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.2);
}

/* 响应式适配 */
@media (max-width: 576px) {
  .logo-container {
    margin: 20px 15px;
  }

  .login-logo {
    width: 160px;
    height: 40px;
  }

  .form-card {
    padding: 20px 25px;
    margin: 0 15px;
  }
}
</style>
